<?php

namespace App\Http\Controllers;

use App\Club;
use App\ClubsCheck;
use App\Http\Requests\ClubRequest;
use Illuminate\Http\Request;

class ClubController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Club $model)
    {
        if (!is_null($request->type) && !in_array($request->type, [0, 1]))
            return redirect()->route('club.index')->withStatus('请选择正确的分类');

        if (is_null($request->search))
            // 按分类查看列表
            $clubs = is_null($request->type)
                ? $model->paginate(15)
                : $model->where('status', $request->type)
                ->paginate(15);
        else
            // 搜索
            $clubs = $model
                //                ->where('status', 1)
                //                ->whereIn('rec_status', [1, 2])
                ->where('club_name', 'like', "%{$request->search}%")
                ->paginate(15);

        return view('clubs.index', [
            'clubs' => $clubs,
            'countAll' => $model->count(),
            'countStatus0' => $model->where('status', 0)->count(),
            'countStatus1' => $model->where('status', 1)->count(),
        ]);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param \App\Post $club
     * @return \Illuminate\Http\Response
     */
    public function status(Request $request, Club $club)
    {
        $club->status = $request->get('status');
        $club->save();
        return response(json_encode((object) [
            'status' => $club->status,
            'updated_at' => $club->updated_at->toDateTimeString()
        ]));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('clubs.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(ClubRequest $request, Club $club)
    {
        $club->create($request->merge([
            'status' => 0,
            'image' => preg_replace('/^public\//', '/storage/', $request->imagefile->store('public/images')),
        ])->all());

        return redirect()->route('clubs.index')->withStatus('俱乐部创建成功');
    }

    public function checkList(Request $request)
    {
        if (!is_null($request->type) && !in_array($request->type, [0, 1, 2]))
            return redirect()->route('club.index')->withStatus('请选择正确的分类');

        $clubId = $request->user()->club_id;
        $model = new ClubsCheck();

        if (is_null($request->search))
            // 按分类查看列表
            $list = is_null($request->type)
                ? $model->with('user')
                ->where('club_id', $clubId)
                ->whereIn('status', [0, 1])
                ->orderBy('status', 'asc')
                ->paginate(15)
                : $model->with('user')
                ->where([['club_id', $clubId], ['status', $request->type]])
                ->paginate(15);
        else
            // 搜索
            $list = $model->with('user')
                ->where('club_id', $clubId)
                ->whereIn('status', [0, 1])
                ->whereHas('user', function ($query) use ($request) {
                    $query->where('username', 'like', "%{$request->search}%");
                })
                ->paginate(15);

        return view('clubs.checks.list', [
            'list' => $list,
            //            'countAll' => $model->count(),
            'countStatus0' => $model->where('club_id', $clubId)->where('status', 0)->count(),
            'countStatus1' => $model->where('club_id', $clubId)->where('status', 1)->count(),
            //            'countStatus2' => $model->where('status', 2)->count(),
        ]);
    }

    public function check(Request $request, ClubsCheck $clubscheck)
    {

        $clubscheck->status = $request->get('status');
        $clubscheck->save();
        return response(json_encode((object) [
            'status' => $clubscheck->status,
            //            'updated_at' => $check->updated_at->toDateTimeString()
        ]));
    }
}
